#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->label->setText("密码为空");

    ui->lineEdit_p1->setPlaceholderText("请输入密码");
    ui->lineEdit_p1->setClearButtonEnabled(true);
    ui->lineEdit_p1->setEchoMode(QLineEdit::Password);

    ui->lineEdit_p2->setPlaceholderText("请确认密码");
    ui->lineEdit_p2->setClearButtonEnabled(true);
    ui->lineEdit_p2->setEchoMode(QLineEdit::Password);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_lineEdit_p1_textChanged(const QString &arg1)
{
    (void)arg1;
    compare();
}

void Widget::on_lineEdit_p2_textChanged(const QString &arg1)
{
    (void)arg1;
    compare();
}

// 提出相同逻辑代码，减少冗余
void Widget::compare()
{
    const QString s1 = ui->lineEdit_p1->text();
    const QString s2 = ui->lineEdit_p2->text();

    if((!s1.isEmpty() && !s2.isEmpty()) && (s1 == s2))
    {
        ui->label->setText("密码一致！");
    }
    else
    {
        ui->label->setText("密码不一致！");
    }
}

// 通过按钮切换文本框的显示模式
void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_p1->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_p2->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_p1->setEchoMode(QLineEdit::Password);
        ui->lineEdit_p2->setEchoMode(QLineEdit::Password);
    }
}
